/* http://localhost/test2/test.php?jo=1
 * url = new url;
 * OR url = new url('http://localhost/test2/just.php?jo=1');
 * url.setroute(['controller','action','id']);
 * action 	= url.get('route','action');	//test.php
 * OR action= url.get('route');
 * url 		= url.get('url');				//http://localhost/test2/test.php?jo=1
 * href 	= url.get('href');				//localhost/test2/test.php?jo=1
 * protocol = url.get('protocol');			//http
 * domain 	= url.get('domain');			//localhost
 * uri 		= url.get('uri');				//test2/test.php
 * query 	= url.get('query','jo');		//1
 * OR query	= url.get('query');
 * */
var url = function(givenUrl) {
	if (!(this.url = givenUrl)){
		if (!(this.url = document.URL)) {
			if (!(this.url = document.location.href)) {
				if (!(this.url = self.location.href)) {
					if (!(this.url = document.location)) {
						this.url = null;
					}
				}
			}
		}
	}
	
	this.box(this.url);
	
	parts = this.url.split('://');
	this.protocol = parts[0].toLowerCase();
	this.href = parts[1];
	this.route = this.href.split('/');
	this.domain = this.route.shift();
	
	last = this.last().split('?');
	if (1 < last.length){
		this.queryString = last[1];
		this.last(last[0]);
		query = last[1].split('&');
		for (i in query){
			parts = query[i].split('=');
			this.query[parts[0]] = parts[1];
		}
	}
	
	this.uri = this.route.join('/');
}
url.prototype = {
	url:null,
	href : null,
	protocol : null,
	domain : null,
	uri : null,
	route : [],
	routeString : null,
	query : [],
	queryString : null,
	box:function(url){//包装url字串
		if ('string' !== typeof url){
			url = this.url;
		}
		url = url.toLowerCase();
		url = this.addprotocol(url);
		this.url = url;
	},
	addprotocol:function(url){//如果没有协议字串，添加协议字串
		if ('string' !== typeof url){
			url = this.url;
		}
		if ((0 === url.indexOf('http://')) || 0 === url.indexOf('https://')){
			return url;
		}else{
			return 'http://'+url;
		}
		return false;
	},
	setroute:function(routeRule){//路由设置
		if ('object' !== typeof routeRule || !routeRule.length){
			return false;
		}
		route = [];
		routeString = [];
		for (i in routeRule){
			if (this.route[i]){
				route[routeRule[i]] = this.route[i];
				routeString.push(routeRule[i]+'='+this.route[i]);
			}
		}
		this.route = route;
		this.routeString = routeString.join(';');
	},
	last:function(value) {//返回url最后一个斜杠后的、请求字串前的，通常是页面文件名位置的字串
		if (!this.route || 0 == this.route.length) {
			return '';
		}
		if (value) {
			this.route[this.route.length - 1] = value;
		}
		return this.route[this.route.length - 1];
	},
	get:function(part,index){//根据参数获取url各个被解析的部分
		if ('string' !== typeof part){
			return false;
		}
		if (!this[part]){
			return false;
		}
		if (index){
			if ('number' === typeof index){
				index += 1;
			}
			if (!this[part][index]){
				return false;
			}
			return this[part][index];
		}
		if ('object' === typeof this[part]){
			return this[part+'String'];
		}
		return this[part];
	}
};